<template>
    <div class="page-100 login-register-part">
        <yd-cell-item style="padding-bottom: 20px">
            <img  slot="left" class="login-img" src="../../assets/loginmobile.png">
            <input slot="right" type="number" placeholder="手机号" v-model="mobile" maxlength="11"
                   oninput="if (value.length>11) value=value.slice(0,11)">
        </yd-cell-item>
        <yd-cell-item style="padding-bottom: 20px">
            <img  slot="left" class="login-img" src="../../assets/loginyanz.png">
            <input slot="right" type="text" placeholder="短信验证码" v-model="captcha"  oninput="if (value.length>6) value=value.slice(0,6)">
            <yd-sendcode slot="right"
                         v-model="startcode"
                         @click.native="sendCaptcha"
                         type="primary"
            ></yd-sendcode>

        </yd-cell-item>
        <yd-cell-item >
            <img  slot="left" class="login-img" src="../../assets/loginpassword.png">
            <!--//注册或激活账号 userState 0 是需要注册 1是需要激活 2是重置密码-->
            <yd-input v-if="this.userState === '2'" slot="right" type="password" v-model="newpwd" placeholder="新密码" maxlength="20" :minlength="6"></yd-input>
            <yd-input v-else slot="right" type="password" v-model="newpwd" placeholder="设置密码" maxlength="20" :minlength="6"></yd-input>

        </yd-cell-item>

        <p class="center-p-mgtop" v-if="this.userState === '2'">请设置8-20位新密码，密码必须同时包含字母和数字。</p>
        <p class="center-p-mgtop" v-else >请设置8-20位登录密码，密码必须同时包含字母和数字。</p>
        <yd-button  size="large" type="primary" @click.native="handleClick" style="background-color:#3cc8a8;color:white;height: 40px">确定</yd-button>

    </div>
</template>

<script>
    import {checkMobile,captcha,registry1,restPWCaptcha,resetPassword} from "../../pcof/post";
    import {authToken,userInfoData,registryFlag,mobile} from '../../globalVariables'
    export default {
        name: "loginResetPwd",
        data() {
            return {
                startcode: false,
                mobile:JSON.parse(localStorage.getItem(mobile)),
                captcha: '',
                newpwd: '',
                userState: '0'
            }
        },
        created(){
            //接受上个页面传值 在实例创建完成后被立即调用
            this.userState = this.$route.params.userState;
            console.log(this.userState);
            //注册或激活账号 userState 0 是需要注册 1是需要激活 2是重置密码
            if (this.userState === '0'||this.userState === '1') {

            }
        },
        methods: {


            /**发送验证码*/
            async sendCaptcha() {
                if(this.mobile===''||this.mobile===null)
                {
                    this.$dialog.toast({
                        mes: '请输入手机号',
                        timeout: 1500
                    });
                    return;
                }
                if (this.mobile.length < 11) {
                    this.$dialog.toast({
                        mes: '手机输入有误！',
                        timeout: 1500
                    });
                    return;
                }

                //注册或激活账号 userState 0 是需要注册 1是需要激活 2是重置密码
                 if (this.userState === '0'||this.userState === '1') {
                //     //校验手机号application/checkMobile
                    var mobileResponse = await checkMobile(this.mobile);

                    localStorage.setItem(authToken,mobileResponse.data.authtoken);

                    if(mobileResponse.data.code === 200 )//正常的手机号
                    {
                        localStorage.setItem(mobile,JSON.stringify
                        (this.mobile));
                        this.$router.push({name: 'loginPhone'});
                        this.$dialog.toast({
                            mes:  '手机号已经注册请直接登录',
                            timeout: 1500
                        });

                    }else if (mobileResponse.data.code === 201 || mobileResponse.data.code === 202) {

                        this.$dialog.loading.open('发送中...');
                        // 注册/激活页面发送验证码
                        // application/captcha

                        var captcharesponse = await captcha(this.mobile);
                        if (captcharesponse.data.code === 200) {

                            setTimeout(() => {

                                this.startcode = true;
                                this.$dialog.loading.close();
                                this.$dialog.toast({
                                    mes: '已发送',
                                    timeout: 1500
                                });

                            }, 1000);
                        }else
                        {
                            this.$dialog.loading.close();
                            this.$dialog.toast({
                                mes: captcharesponse.data.msg,
                                timeout: 1500
                            });
                        }
                    }else {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: mobileResponse.data.msg,
                            timeout: 1500
                        });
                    }


                 } else if(this.userState === '2') {

                    //重置密码校验手机号application/restPWCaptcha
                    var mobileResponse1 = await checkMobile(this.mobile);
                    localStorage.setItem(authToken,mobileResponse1.data.authtoken);
                     if (mobileResponse1.data.code === 200) {
                        this.$dialog.loading.open('发送中...');
                        //注册/激活页面发送验证码
                        //application/captcha
                        var restresponse = await restPWCaptcha(this.mobile);
                        console.log(restresponse);
                        if (restresponse.data.code === 200) {
                            setTimeout(() => {

                                this.startcode = true;
                                this.$dialog.loading.close();
                                this.$dialog.toast({
                                    mes: '已发送',
                                    timeout: 1500
                                });

                            }, 1000);

                        }  else {
                            this.$dialog.loading.close();
                            this.$dialog.toast({
                                mes: restresponse.data.msg,
                                timeout: 1500
                            });
                        }
                    } else {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: mobileResponse1.data.msg,
                            timeout: 1500
                        });
                    }
                }

            },
            /**发送修改手机密码、注册或激活*/
            async handleClick() {
                if(this.mobile===''||this.mobile===null)
                {
                    this.$dialog.toast({
                        mes: '请输入手机号',
                        timeout: 1500
                    });
                    return;
                }
                if (this.mobile.length < 11) {
                    this.$dialog.toast({
                        mes: '手机输入有误！',
                        timeout: 1500
                    });
                    return;
                }
                //注册或激活账号 userState 0 是需要注册 1是需要激活 2是重置密码
                // 注册/激活页面第一步
                if (this.startcode == false) {
                    this.$dialog.toast({
                        mes: '请发送验证码！',
                        timeout: 1500
                    });
                    return;
                }

                if(this.captcha===''||this.captcha===null)
                {
                    this.$dialog.toast({
                        mes: '请输入验证码',
                        timeout: 1500
                    });
                    return;
                }
                if (this.userState === '0'||this.userState === '1') {

                    if(this.newpwd===''||this.newpwd===null)
                    {
                        this.$dialog.toast({
                            mes: '请设置登录密码',
                            timeout: 1500
                        });
                        return;
                    }

                }else
                {
                    if(this.newpwd===''||this.newpwd===null)
                    {
                        this.$dialog.toast({
                            mes: '请设置新密码',
                            timeout: 1500
                        });
                        return;
                    }

                }
                var exp=/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/;

                if(!exp.test(this.newpwd))
                {
                    this.$dialog.toast({
                        mes: '密码格式错误',
                        timeout: 1500
                    });
                    return;
                }
                if (this.userState === '0'||this.userState === '1') {
                    this.$dialog.loading.open('加载中...');
                    var registryresponse = await registry1(this.mobile,this.captcha,this.newpwd);
                    if (registryresponse.data.code === 200) {
                        this.$dialog.loading.close();
                        //isregistry 0是注册页 1是激活页 3是修改资料页
                        if(registryresponse.data.flag ==='registry')
                        {
                            localStorage.removeItem(userInfoData);
                            localStorage.setItem(registryFlag,registryresponse.data.flag);
                            //isregistry 0是注册页  3是修改资料页
                            this.$router.push({name: 'loginRegistry', params: {isregistry: '0'}});

                        }else if(registryresponse.data.flag ==='activated')
                        {
                            localStorage.setItem(registryFlag,registryresponse.data.flag);
                            localStorage.setItem(userInfoData,JSON.stringify(registryresponse.data.data));
                            this.$router.push({name: 'loginActive'});
                        }

                    }else
                    {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: registryresponse.data.msg,
                            timeout: 1500
                        });
                    }

                } else if (this.userState === '2') {
                    this.$dialog.loading.open('加载中...');
                    // mobile,captcha,password

                    var resetresponse = await resetPassword(this.mobile,this.captcha,this.newpwd);
                    console.log(resetresponse);
                    if (resetresponse.data.code === 200) {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: '密码重置成功',
                            timeout: 1500
                        });
                        this.$router.push({name: 'loginPhone'});
                    }  else {
                        this.$dialog.loading.close();
                        this.$dialog.toast({
                            mes: resetresponse.data.msg,
                            timeout: 1500
                        });
                    }
                }
            },
        },
    }
</script>

<style lang="scss">
    @import "../login/login.scss";

    .center-p-mgtop {
        text-align: left;
        font-size: 0.25rem;
        margin-left: 0.5rem;
        margin-top: 0.2rem;
        color:#cacaca;
    }

</style>